# modules/skill.py

import mss
import pyautogui
from src.core.template_matcher import TemplateMatcher


class Skill(TemplateMatcher):
    def __init__(self, gameWindow, config: dict, instanceName: str):
        # config 包含: region_x (dict), region_y, region_w, region_h, template_dir
        super().__init__(template_dir=config['template_dir'])

        # 使用 instanceName 从 config['region_x'] 字典中查找对应的 X 坐标
        if 'region_x' not in config:
            raise ValueError("配置中缺少 'region_x' 字段")
        if not isinstance(config['region_x'], dict):
            raise ValueError("'region_x' 应该是一个字典，包含各个技能的 X 坐标")
        if instanceName not in config['region_x']:
            raise ValueError(f"实例名 '{instanceName}' 在 region_x 配置中未找到")

        # 获取对应技能的 region_x 偏移
        local_region_x = config['region_x'][instanceName]

        # 计算全局坐标
        self.region_x = gameWindow.x + local_region_x
        self.region_y = gameWindow.y + config['region_y']
        self.region_w = config['region_w']
        self.region_h = config['region_h']

        self.value_x = config['value_x']
        self.value_y = config['value_y']
        self.value_w = config['value_w']
        self.value_h = config['value_h']

        # 可选：保存其他参数，如 cd_point
        self.cd_point1_x = config['cd_point1_x']
        self.cd_point1_y = config['cd_point1_y']
        self.cd_point1_true_color = tuple(config['cd_point1_true_color'])

        self.cd_point2_x = config['cd_point2_x']
        self.cd_point2_y = config['cd_point2_y']
        self.cd_point2_true_color = tuple(config['cd_point2_true_color'])

        # 可选：保存 instanceName 供后续使用
        self.instanceName = instanceName

        print(f"Skill 实例 '{instanceName}' 初始化成功: "
              f"区域=({self.region_x}, {self.region_y}, {self.region_w}, {self.region_h})")

    def get_value(self):

        screenshot = pyautogui.screenshot(region=(self.region_x + self.value_x,
                                                  self.region_y + self.value_y,
                                                  self.value_w, self.value_h))

        result = self.match_digit(screenshot, threshold=0.9)

        return int(result)
    def is_ready(self):
        try:
            with mss.mss() as sct:
                monitor1 = {"top": self.region_y + self.cd_point1_y, "left": self.region_x + self.cd_point1_x, "width": 1, "height": 1}
                sct_img1 = sct.grab(monitor1)

                monitor2 = {"top": self.region_y + self.cd_point2_y, "left": self.region_x + self.cd_point2_x, "width": 1, "height": 1}
                sct_img2 = sct.grab(monitor2)

            color1 = tuple((sct_img1.raw[2], sct_img1.raw[1], sct_img1.raw[0]))

            color2 = tuple((sct_img2.raw[2], sct_img2.raw[1], sct_img2.raw[0]))

            if color1 == self.cd_point1_true_color or color2 == self.cd_point2_true_color:
                return False
            else:
                return True
        except Exception as e:
            print(f"获取像素颜色失败: {e}")
            return False